/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.ad.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.common.mybatis.mapper.query.QueryType;
import com.jeesite.modules.ad.entity.Ad;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.utils.excel.ExcelExport;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.ad.entity.AdGuest;
import com.jeesite.modules.ad.entity.AdVertiser;
import com.jeesite.modules.ad.service.AdGuestService;
import com.jeesite.modules.ad.service.AdVertiserService;
import com.jeesite.modules.file.entity.FileUpload;
import com.jeesite.modules.file.utils.FileUploadUtils;

/**
 * 广告获客Controller
 * 
 * @author Liu,Wenzhao
 * @version 2019-04-17
 */
@Controller
@RequestMapping(value = "${adminPath}/ad/adGuest")
public class AdGuestController extends BaseController {

	@Autowired
	private AdGuestService adGuestService;
	@Autowired
	private AdVertiserService adVertiserService;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public AdGuest get(String id, boolean isNewRecord) {
		return adGuestService.get(id, isNewRecord);
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("ad:adGuest:view")
	@RequestMapping(value = { "list", "" })
	public String list(AdGuest adGuest, Model model) {
		model.addAttribute("adGuest", adGuest);
		return "modules/ad/adGuestList";
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("ad:adGuest:view")
	@RequestMapping(value = { "listByAd"})
	public String listByAd(String adName,String adId, Model model) {
		AdGuest adGuest = new AdGuest();
		Ad ad =new Ad();
		ad.setName(adName);
		ad.setId(adId);
		adGuest.setAd(ad);
		model.addAttribute("adGuest", adGuest);
		return "modules/ad/adGuestList";
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("ad:adGuest:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<AdGuest> listData(AdGuest adGuest, HttpServletRequest request, HttpServletResponse response) {
		adGuest.setPage(new Page<>(request, response));
		adGuest.setAd_id_gte();
		Page<AdGuest> page = adGuestService.findPage(adGuest);
		return page;
	}

	/**
	 * 查看
	 */
	@RequiresPermissions("ad:adGuest:view")
	@RequestMapping(value = "detail")
	public String detail(AdGuest adGuest, Model model) {
		model.addAttribute("adGuest", adGuest);
		return "modules/ad/adGuestDetail";
	}
	
	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("ad:adGuest:view")
	@RequestMapping(value = "form")
	public String form(AdGuest adGuest, Model model) {
		model.addAttribute("adGuest", adGuest);
		return "modules/ad/adGuestForm";
	}

	/**
	 * 保存广告获客
	 */
	@RequiresPermissions("ad:adGuest:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated AdGuest adGuest) {
		if (!"1".equals(adGuest.getValidStatus())) {
			adGuest.setConsumptionStatus("");
		}
		adGuestService.save(adGuest);
		return renderResult(Global.TRUE, text("保存广告获客成功！"));
	}

	/**
	 * 导出获客
	 */
	@RequiresPermissions("ad:adVertiser:edit")
	@RequestMapping(value = "exportData")
	public void exportData(AdGuest adGuest, Boolean isAll, String ctrlPermi, HttpServletResponse response) {
		// 查询当前广告主
		AdVertiser v = this.adVertiserService.getCurVertiser();
		adGuest.setAdVertiser(v);
		List<AdGuest> list = adGuestService.findList(adGuest);
		String fileName = "获客信息" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
		try (ExcelExport ee = new ExcelExport("获客信息数据", AdGuest.class)) {
			ee.setDataList(list).write(response, fileName);
		}
	}
	
	@RequiresPermissions("ad:adGuest:view")
	@RequestMapping(value = "play")
	public String play(AdGuest adGuest, HttpServletRequest request, Model model) {
		model.addAttribute("adGuest", adGuest);
		String ctx = request.getContextPath(); 
		List<FileUpload> uList = FileUploadUtils.findFileUpload(adGuest.getId(), "adGuest_audio");
		String src = "";
		if(!ListUtils.isEmpty(uList)) {
			FileUpload a = uList.get(0);
			src = ctx + a.getFileUrl();
		}
		model.addAttribute("src", src);
		return "modules/ad/play";
	}
}